package com.itheima.controller;

import com.itheima.Utils.AliOssUtils;
import com.itheima.Utils.JwtUtils;
import com.itheima.pojo.*;
import com.itheima.services.BusinessServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
public class BusinessController {
    @Autowired
    public BusinessServices businessServices;

    @RequestMapping("/Blogin")
    public Result Blogin(@RequestBody User user){
        System.out.println("Blogin");
        User user1=businessServices.Blogin(user);
        if(user1!=null){
            Map<String,Object> claims=new HashMap<>();
            claims.put("name",user1.getName());
            claims.put("password",user1.getPassword());
            String jwt= JwtUtils.generateJwt(claims);
            System.out.println(jwt);
            return Result.success(jwt);
        }
        return Result.error();
    }
    @GetMapping("showEmp")
    public Result showEmp(){
        System.out.println("showEmp");
        List<Emp> emps=businessServices.showEmp();
        return Result.success(emps);
    }
    @PostMapping("/conShowEmp")
    public Result conShowEmp(@RequestBody EmpForm empForm){
        System.out.println("conShowEmp");
        System.out.println(empForm.getName());
        List<Emp> emps=businessServices.conShowEmp(empForm.getName());
        return Result.success(emps);
    }
    @GetMapping("/changeEmp1")
    public Result changeEmp1(Long empId){
        System.out.println("changeEmp1");
        System.out.println(empId);
        Emp emp=businessServices.changeEmp1(empId);
        return Result.success(emp);
    }
    @PostMapping("/changeEmp2")
    public Result changeEmp2(@RequestBody Emp emp){
        System.out.println("changeEmp2");
        Result result=businessServices.changeEmp2(emp);
        return result;
    }
    @GetMapping("/changeState")
    public Result changeState(String state,Long empId){
        System.out.println("changeState");
        businessServices.changeState(state,empId);
        return Result.success();
    }

    @PostMapping("/addEmp")
    public Result addEmp(@RequestBody Emp emp){
        System.out.println("addEmp");
        Result result=businessServices.addEmp(emp);
        return result;
    }

    @GetMapping("/showCategory")
    public Result showCategory(){
        System.out.println("showCategory");
        List<FoodCategory> categories=businessServices.showCategory();
        return Result.success(categories);
    }
    @GetMapping("/showFood")
    public Result showFood(){
        System.out.println("showFood");
        List<Food> foods=businessServices.showFood();
        return Result.success(foods);
    }
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {
        System.out.println("upload");
        String originalFilename=file.getOriginalFilename();
        String filename= UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        InputStream inputStream=file.getInputStream();
        String url= AliOssUtils.upload(filename,inputStream);
        return Result.success(url);
    }
    @GetMapping("/changeFood1")
    public Result changeFood1(Long foodId){
        System.out.println("changeFood1");
        Food food=businessServices.changeFood1(foodId);
        return Result.success(food);
    }
    @PostMapping("/changeFood2")
    public Result changeFood2(@RequestBody Food food){
        System.out.println("changeFood2");
        businessServices.changeFood2(food);
        return Result.success();
    }

    @PostMapping("/conShowFood")
    public Result conShowFood(@RequestBody Food food){
        System.out.println("conShowFood");
        List<Food> foods=businessServices.conShowFood(food);
        return Result.success(foods);
    }
   @GetMapping("/deleteFood")
    public Result deleteFood(Long foodId){
       System.out.println("deleteFood");
        businessServices.deleteFood(foodId);
        return Result.success();
   }
   @PostMapping("/addFood")
    public Result addFood(@RequestBody Food food){
        System.out.println("addFood");
        businessServices.addFood(food);
        return Result.success();
   }
   @GetMapping("/changeFoodState")
    public Result changeFoodState(Long foodId ,String foodState){
        System.out.println("changeFoodState");
        businessServices.changeFoodState(foodId,foodState);
        return Result.success();
   }
    @PostMapping("/showOrders")
    public Result showOrders(@RequestBody OrderForm form,int currentPage,int pageSize){
        System.out.println("showOrders");
        PageBean pageBean=businessServices.showOrders(form,currentPage,pageSize);
        System.out.println(pageBean);
        return Result.success(pageBean);
    }

    @GetMapping("/cancelOrder")
    public Result cancelOrder(Long orderId){
        businessServices.cancelOrder(orderId);
        return Result.success();
    }

    @GetMapping("/acceptOrder")
    public Result acceptOrder(Long orderId){
        businessServices.acceptOrder(orderId);
        return Result.success();
    }

    @GetMapping("/changeCategory1")
    public Result changeCategory1(Long categoryId){
        System.out.println("changeCategory1");
        FoodCategory category=businessServices.changeCategory1(categoryId);
        return Result.success(category);
    }

    @PostMapping("/changeCategory2")
    public Result changeCategory2(@RequestBody FoodCategory categoryForm){
        System.out.println("changeCategory2");
        businessServices.changeCategory2(categoryForm);
        return Result.success();
    }

    @GetMapping("/deleteCategory")
    public Result deleteCategory(Long categoryId){
        System.out.println("deleteCategory");
        businessServices.deleteCategory(categoryId);
        return Result.success();
    }

    @GetMapping("/addCategory")
    public Result addCategory(String category){
        System.out.println("addCategory");
        businessServices.addCategory(category);
        return Result.success();
    }
}
